import os


# 将字节数转换为更友好的格式
def convert_bytes(size_bytes):
    for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
        if size_bytes < 1024:
            return f"{size_bytes:.2f} {unit}"
        size_bytes /= 1024


def get_folder_size(folder_path, conv_to_formatted_str=True):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(folder_path):
        for filename in filenames:
            filepath = os.path.join(dirpath, filename)
            total_size += os.path.getsize(filepath)
    return total_size if not conv_to_formatted_str else convert_bytes(total_size)


if __name__ == '__main__':
    folder_path = "../tempdir/recorded_states/screen_image/test__2024-01-25 02-35-38"  # 替换为实际文件夹路径

    size_bytes = get_folder_size(folder_path, conv_to_formatted_str=True)
    # size_formatted = convert_bytes(size_bytes)

    # print(f"The size of the folder '{folder_path}' is: {size_formatted}")
    print(f"The size of the folder {size_bytes}")

